package com.feiwo.nonego.rpc.common.util;

import java.util.stream.IntStream;

/**
 * @author feiwo 2025-08-05
 */
public class SerializationUtils {

    private static final String PADDING_STRING = "0";

    /**
     * 约定序列化类型最大长度为16
     */
    public static final int MAX_SERIALIZATION_TYPE_COUNT = 16;

    public static String getSerializationType(String serializationType) {
        return paddingString(serializationType, MAX_SERIALIZATION_TYPE_COUNT);
    }

    public static String paddingString(String str, int len) {
        str = transNullToEmpty(str);
        if (str.length() >= len) return str;
        var paddingCount = len - str.length();
        var paddingString = new StringBuilder(str);
        IntStream.range(0, paddingCount).forEach(i -> {
            paddingString.append(PADDING_STRING);
        });
        return paddingString.toString();
    }

    /**
     * 字符串去0 操作
     * @param str 原始字符串
     * @return 去0后的字符串
     */
    public static String subString(String str){
        str = transNullToEmpty(str);
        return str.replace(PADDING_STRING, "");
    }

    public static String transNullToEmpty(String str){
        return str == null ? "" : str;
    }
}
